package controllers;

import java.util.List;

import play.data.validation.Required;
import play.modules.paginate.ValuePaginator;
import play.mvc.With;

import common.FBSecure;

import models.CommentEpisode;
import models.CommentTopic;
import models.Episode;
import models.Serie;
import models.Topic;
import models.User;
import models.Video;

@With(FBSecure.class)
public class Episodes extends FBController{

	public static void episodes(long id){		

		setFbIdandName();

		User user = getUser();
		Serie series = Serie.findById(id);

		List<Episode> episodes = Episode.getAllEpisodes(id); 

		render(user, series, episodes);
	}

	public static void details(long id, long eid) {

		setFbIdandName();

		User user = getUser();
		Serie series = Serie.findById(id);

		Episode episode = Episode.findById(eid);

        ValuePaginator paginator = new ValuePaginator(episode.getCommentEpisodeList());
		paginator.setPageSize(5);
		
		render(user, series, episode, paginator);

	}
	
    public static void postComment(long sid, long eid, @Required String comment) {
    	
    	setFbIdandName();
    	
    	User user = getUser();
    	Serie series = Serie.findById(sid);
    	
        Episode episode = Episode.findById(eid);

        ValuePaginator paginator = new ValuePaginator(episode.getCommentEpisodeList());
		paginator.setPageSize(5);
		
        if (validation.hasErrors()) {
        	renderTemplate("Episodes/details.html", series, episode, user, paginator);
		}
        
        user.addComment(episode, comment);
        
        user.save();
        
        details(sid, eid);
    }
    
    public static void deleteCommentFromEpisode(long id, long eid, long cid) {
    	
    	User user = getUser();
        CommentEpisode comment = CommentEpisode.findById(cid);        
        
        if (user.getComments().contains(comment)){
	        user.removeComment(comment);
	        comment.delete();
	        user.save();
        }
        
        details(id, eid);
    }

}